package com.iotp.controller.app;

import com.iotp.entity.base.DataMessage;
import com.iotp.entity.expert.Expert;
import com.iotp.entity.knowledge.KnowledgeWithBLOBs;
import com.iotp.entity.user.CommUser;
import com.iotp.entity.userUserRef.UserUserRef;
import com.iotp.entity.video.Video;
import com.iotp.service.expert.ExpertService;
import com.iotp.service.knowledge.KnowledgeService;
import com.iotp.service.user.UserService;
import com.iotp.service.userUserRef.UserUserRefService;
import com.iotp.service.video.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportResource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/app")
public class AppController {
    @Autowired
    private KnowledgeService knowledgeService;
    @Autowired
    private ExpertService expertService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserUserRefService userUserRefService;
    @Autowired
    private VideoService videoService;

    /**
     * 知识库，种养问题
     * @return
     * @throws Exception
     */
    @PostMapping("/getKnowledgeList")
    public DataMessage getKnowledgeList() throws Exception{
        DataMessage dm = new DataMessage();
        KnowledgeWithBLOBs query = new KnowledgeWithBLOBs();
        query.setkTypeId(5L);//知识
        List<KnowledgeWithBLOBs> list = this.knowledgeService.queryList(query);
        dm.setResult(0);
        dm.setData(list);
        dm.setMessage("获取成功");
        return dm;
    }

    @PostMapping("/getExpertList")
    public DataMessage getExpertList() throws Exception{
        DataMessage dm = new DataMessage();
        List<Expert> list = this.expertService.queryList(new Expert());
        if(null != list && !list.isEmpty()){
            Iterator it = list.iterator();
            while (it.hasNext()){
                Expert e = (Expert)it.next();
                if(e.getUserId() == null || e.getUserId().longValue() == 0L){
                    it.remove();
                }else{
                    UserUserRef u = this.userUserRefService.queryById(e.getUserId());
                    if(u == null){
                        it.remove();
                    }else{
                        CommUser cu = this.userService.queryUserById(u.getOldUserId());
                        if(cu == null){
                            it.remove();
                        }else{
                            if(cu.getImVideoAccount() == null || "".equals(cu.getImVideoAccount())){
                                it.remove();
                            }else{
                                e.setAccount(cu.getItCode());
                                e.setImVideoAccount(cu.getImVideoAccount());
                            }
                        }
                    }
                }
            }
        }
        dm.setResult(0);
        dm.setData(list);
        dm.setMessage("获取成功");
        return dm;
    }

    @PostMapping("/getVideoList")
    public DataMessage getVideoList(){
        DataMessage dm = new DataMessage();
        List<Video> res = this.videoService.selectList(new Video());
        dm.setResult(0);
        dm.setData(res);
        dm.setMessage(null);
        return dm;
    }
}
